package cn.cyh.queue_service.config;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

/**
 * RestTemplate配置类
 * 用于配置与外部服务通信的RestTemplate
 */
@Configuration
@RequiredArgsConstructor
public class RestTemplateConfig {

    private final FaceRecognitionProperties faceProperties;

    /**
     * 配置RestTemplate Bean
     *
     * @param builder RestTemplateBuilder
     * @return 配置好的RestTemplate实例
     */
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder
                .requestFactory(this::clientHttpRequestFactory)
                .setConnectTimeout(Duration.ofMillis(faceProperties.getConnectTimeout()))
                .setReadTimeout(Duration.ofMillis(faceProperties.getReadTimeout()))
                .build();
    }

    /**
     * 配置ClientHttpRequestFactory
     *
     * @return ClientHttpRequestFactory实例
     */
    private ClientHttpRequestFactory clientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(faceProperties.getConnectTimeout());
        factory.setReadTimeout(faceProperties.getReadTimeout());
        return factory;
    }
} 